﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cotrobaie.UsersWebService;

namespace Cotrobaie
{
    public partial class Comments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ButtonStatistics.Attributes.Add("onMouseOut", "this.src='/images/comm.png'");
            ButtonStatistics.Attributes.Add("onMouseOver", "this.src='/images/ocomm.png'");
            ButtonDescr.Attributes.Add("onMouseOut", "this.src='/images/recc.png'");
            ButtonDescr.Attributes.Add("onMouseOver", "this.src='/images/orecc.png'");
            ButtonComments.Attributes.Add("onMouseOut", "this.src='/images/acc.png'");
            ButtonComments.Attributes.Add("onMouseOver", "this.src='/images/oacc.png'");
             //if user logged
            if (Session["UserID"] == null)
                Response.Redirect("~/Home.aspx");
            else
            {
                if (!Page.IsPostBack) Session["CommentsCurrPage"] = null;
                if (!Page.IsPostBack) printComments();
                
            }
        }

        private void printComments()
        {
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                int intrariPagina = 5;
                if (Session["CommentsCurrPage"] == null) Session["CommentsCurrPage"] = 0;

                var result = serviciu.GetUserComments((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(), (int)Session["CommentsCurrPage"], intrariPagina);
                if (result.Commenturi.Count() == 0 && (int)Session["CommentsCurrPage"] == 0)
                {
                    Literal1.Text = "No comments added yet.";
                    ButtonNextPage.Visible = false;
                    ButtonPrevPage.Visible = false;
                }
                else
                {
                    PlaceHolder1.Controls.Clear();
                    Table table = new Table();
                    PlaceHolder1.Controls.Add(table);

                    
                    if (result.Commenturi.Count() < intrariPagina) { ButtonNextPage.Enabled = false; ButtonNextPage.CssClass = "button3"; }
                    if ((int)Session["CommentsCurrPage"] == 0) { ButtonPrevPage.Enabled = false; ButtonPrevPage.CssClass = "button3"; }
                    LabelCurrentPage.Text = ((int)Session["CommentsCurrPage"] + 1).ToString();

                    foreach (var c in result.Commenturi)
                    {
                        var res = serviciu.GetProdusByID(c.product_id);
                        var produs = res.Produse.First();
                        TableRow tr = new TableRow();
                        tr.Style.Add("width", "500px");
                        TableCell tc = new TableCell();
                        Label lab = new Label();

                        tc = new TableCell();
                        HyperLink hp = new HyperLink();
                        hp.Text = "<b>" + c.titlu + "</b>  -  " + produs.name;
                        hp.NavigateUrl = produs.linkSite;
                        hp.CssClass = "blgrlink";
                        tc.Controls.Add(hp);

                        tc.Style.Add("padding-right", "3px");
                        tr.Cells.Add(tc);

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Controls.Add(lab);
                        tc.Width = 80;
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = c.data.ToString();
                        tc.Controls.Add(lab);
                        tr.Cells.Add(tc);

                        table.Rows.Add(tr);

                        tr = new TableRow();

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = c.comment;
                        tc.Controls.Add(lab);
                        tr.Cells.Add(tc);
                        tc.Style.Add("width", "350px");

                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Controls.Add(lab);
                        tc.Width = 80;
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        tc = new TableCell();
                        Button but = new Button();
                        but.Text = "Remove";
                        but.CssClass = "bigbutton";
                        but.Click += onClickRemove;
                        but.CommandName = "remove";
                        but.CommandArgument = c.ID.ToString();

                        tc.Controls.Add(but);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);

                        //empty row
                        tr = new TableRow();
                        tc = new TableCell();
                        lab = new Label();
                        lab.Text = "&nbsp;";
                        tc.Controls.Add(lab);
                        tr.Cells.Add(tc);
                        table.Rows.Add(tr);
                    }

                }
            }
        }

        public void onClickRemove(object sender, EventArgs e)
        {
            
            using (UsersSoapClient serviciu = new UsersSoapClient("UsersSoap"))
            {
                Button but = (Button)sender;
                var result = serviciu.RemoveUserComment((int)Session["UserID"], Session["UserUsername"].ToString(), Session["UserParola"].ToString(),int.Parse(but.CommandArgument));
                if (result.Success) Page_Load(null, null);
            }
        }

        protected void ButtonPrevPage_Click(object sender, EventArgs e)
        {
            if (Session["CommentsCurrPage"] != null)
                Session["CommentsCurrPage"] = (int)Session["CommentsCurrPage"] - 1;
            else Session["CommentsCurrPage"] = 0;
            ButtonNextPage.Enabled = true;
            ButtonNextPage.CssClass = "button2";
            printComments();
        }

        protected void ButtonNextPage_Click(object sender, EventArgs e)
        {
            if (Session["CommentsCurrPage"] != null)
                Session["CommentsCurrPage"] = (int)Session["CommentsCurrPage"] + 1;
            else Session["CommentsCurrPage"] = 1;
            ButtonPrevPage.Enabled = true;
            ButtonPrevPage.CssClass = "button2";
            printComments();
        }


        public void tabClick(object sender, EventArgs e)
        {
            ImageButton but = (ImageButton)sender;

            if (but.ID == "ButtonDescr")
            {
                Response.Redirect("~/Recommendations.aspx");
            }

            else if (but.ID == "ButtonComments")
            {
                Response.Redirect("~/MyAccount.aspx");
            }

            else if (but.ID == "ButtonStatistics")
            {
                Response.Redirect("~/Comments.aspx");
            }

        }

        protected void goHome(object sender, EventArgs e)
        {
            Response.Redirect("~/Home.aspx");
        }
    }
}